#include <iostream>
#include <vector>


template <class T>
class UnionFindSet
{
public:
	UnionFindSet(const size_t size = 0)
		:_set(size, -1)
	{}

	void Union(const size_t pos1, const size_t pos2)
	{
		size_t root1 = findRoot(pos1);
		size_t root2 = findRoot(pos2);
		if (root1 != root2)
		{
			_set[root1] += _set[root2];
			_set[root2] = root1;
		}
	}

	size_t Size()
	{
		size_t result = 0;
		for (auto& item : _set)
		{
			if (item < 0)
			{
				++result;
			}
		}
		return result;
	}

	size_t findRoot(const size_t pos)
	{
		while (_set[pos] >= 0)
		{
			pos = _set[pos];
		}
		return pos;
	}

private:
	std::vector<T> _set;
};